//
//  LHSightView.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/13.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHSightView.h"
#import "LHSightTableViewCell.h"
#import "LHSightModel.h"
#import "UIImageView+WebCache.h"

@interface LHSightView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)UITableView *detailTabelView;
@end

@implementation LHSightView

- (void)dealloc
{
    [_detailTabelView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self createTableView];
    }
    return self;
}

- (void)createTableView
{
    self.detailTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 64) style:UITableViewStylePlain];
    _detailTabelView.delegate = self;
    _detailTabelView.dataSource  = self;
    [_detailTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_detailTabelView setAllowsSelection:NO];
    [_detailTabelView registerClass:[LHSightTableViewCell class] forCellReuseIdentifier:@"sightReuse"];
    [self addSubview:_detailTabelView];
    [_detailTabelView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sightModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return self.frame.size.height / 3 + [[self class]MyheightWithText:[self.sightModelArr[indexPath.row] desc]] + [[self class]MyheightWithText:[self.sightModelArr[indexPath.row] title]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHSightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sightReuse"];
    [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:[self.sightModelArr[indexPath.row] img]] placeholderImage:[UIImage imageNamed:@"holder"]];
    cell.backgroundColor = [UIColor colorWithRed:0.975 green:1.000 blue:0.959 alpha:1.000];
    cell.titleLabel.text = [self.sightModelArr[indexPath.row] title];
    cell.introduceText = [self.sightModelArr[indexPath.row] desc];
    cell.titleText = [self.sightModelArr[indexPath.row] title];
    return cell;
}

+ (CGFloat)MyheightWithText:(NSString *)text
{
    // 创建一个字典保存文本属性
    // 保存文本文字大小
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    
    // 预设一个尺寸, 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 1000);
    
    // 根据文本信息获得一个CGRect
    // 参数1:尺寸范围  参数2:文本属性  参数3:按照什么方式获取Rect  参数4:nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
}
@end
